package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.ImportExeclUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author spt
 * @description 预约设置控制器
 * @date
 **/
@RestController
@RequestMapping("/ordersetting")
@Slf4j
@Api(tags = "后台预约设置接口")
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;

    //上传 预约设置文件
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile){
        //获取原始文件名  excel.xlsx
        String originalFilename = excelFile.getOriginalFilename();
        //获取文件后缀名 即文件类型
        String fileType = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        //设置xls文件类型为true
        boolean isExcel2003 = true;
        if("xlsx".equals(fileType)){
            isExcel2003 = false;
        }
        //调用工具类方法读取excel文件
        try {
            List<List<String>> list = ImportExeclUtils.read(excelFile.getInputStream(), isExcel2003);
            //保存list到数据库
            orderSettingService.addList(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.IMPORT_ORDERSETTING_FAIL);
        }

        return new Result(true,MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 任务5-根据月份查询数据
     *
     * @param year  年
     * @param month 月
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    @ApiOperation(value = "按月份查询回显")
    public Result getOrderSettingByMonth(@RequestParam Integer year, @RequestParam Integer month) {
        //调用RPC查询数据
        List<OrderSetting> orderSettings = orderSettingService.getOrderSettingByMonth(year, month);
        //转换成前端需要的结构
        List<Map<String, Object>> voList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            //创建用于日期处理的工具类Calendar对象
            Calendar cal = Calendar.getInstance();
            //设置时间为预约日期
            cal.setTime(orderSetting.getOrderDate());
            Map<String, Object> map = new HashMap<>();
            //设置日期限-当月第几天
            map.put("date", cal.get(Calendar.DAY_OF_MONTH));
            //设置数量-当天允许的预约数量
            map.put("number", orderSetting.getNumber());
            //设置已经预约的人数
            map.put("reservations", orderSetting.getReservations());
            voList.add(map);
        }
        return new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS, voList);
    }

    /**
     * 任务6-基于日历实现预约设置
     * @return
     */
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.addTimeAndNumber(orderSetting);
        return new Result(true, MessageConst.ORDERSETTING_SUCCESS);
    }
}

